package com.test;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

/**
 * Created by zhaopeng on 2/28/17.
 */
public class ChramLogStatic extends ReadFileByLine {

    private Map<Integer, String> tagLab = new HashMap();


    @Override
    protected void beginReadFile() {
        tagLab.clear();
    }

    @Override
    protected void doReadAction(String line) {
        String[] words = line.split(Pattern.quote("\t"));

        if (words.length < 2)
            return;
        String strTag = words[0].trim();
        int tag = Integer.parseInt(strTag);
        tagLab.put(tag, words[1]);
    }

    @Override
    protected void doFileDone() {
       /* for (Integer key : tagLab.keySet()) {
            System.out.println(key + tagLab.get(key));
        }*/
    }

    public Map<Integer, String> getTagLab() {
        return tagLab;
    }

    public static void main(String[] args) throws Exception {
        if (4 != args.length)
            return;
        ChramLogStatic read = new ChramLogStatic();
        read.readFile(args[0]);

        StaticTagNum tagNum = new StaticTagNum(read.getTagLab());

        String mzhenLab = args[2];
        String charmLab = args[3];
        //String outputPath = args[2];
        tagNum.setLab(mzhenLab, charmLab);

        tagNum.readFile(args[1]);
    }

}


class StaticTagNum extends ReadFileByLine {
    private Map<Integer, Integer> tagNum = new HashMap();
    private Map<Integer, String> tagLab;
    private String mzhenLab;
    private String charmLab;
    //private String output;

    int[][] TAG_LIST = {{562, 563},
            {564, 565, 566, 567, 568, 569, 570, 571, 12021, 572},
            {11261, 11262, 11263, 11264, 11265},
            {11266, 11267, 11268, 11269, 11270},
            {11271, 11272, 11273, 11274}
    };

    public void setLab(String mzhen, String charm) {
        mzhenLab = mzhen;
        charmLab = charm;
        // this.output = output;
    }

    public StaticTagNum(Map<Integer, String> map) {
        tagNum.clear();
        tagLab = map;
    }

    @Override
    protected void doReadAction(String line) {
        String[] words = line.split(Pattern.quote("\t"));
        if (2 != words.length)
            return;
        for (String str : words[1].split(Pattern.quote(","))) {
            int tag = Integer.parseInt(str);

            if (tagNum.containsKey(tag)) {
                tagNum.put(tag, tagNum.get(tag) + 1);
            } else {
                tagNum.put(tag, 1);
            }
        }
    }

    @Override
    protected void doFileDone() {
        StringBuilder html = new StringBuilder();

        html.append("<table border frame=box rules=all >");
        html.append("<tr bgcolor=\"black\" style=\"color:white\" >");
        html.append("<td>TID</td><td>标签名</d><td>秒针DMP计算版本</td><td>昌荣ID版本</td><td>标签量级</td><td>模型量级</td>");
        html.append("</tr>");

        for (int[] array1 : TAG_LIST) {
            int sum = 0;
            for (int i : array1) {
                sum += tagNum.get(i);
            }

            for (int i : array1) {
                html.append("<tr>");
                html.append("<td>" + i + "</td>" + "<td>" + tagLab.get(i) + "</td>" + "<td>" + mzhenLab
                        + "</td>" + "<td>" + charmLab + "</td>" + "<td>" + tagNum.get(i) + "</td>" + "<td>" + sum + "</td>");
                html.append("<//tr>");
            }
        }
        html.append("</table>");

        try {
            /*FileWriter writer = new FileWriter(output);
            writer.write(html.toString());
            writer.close();*/
            sendmail(html);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendmail(StringBuilder msg) throws Exception {
        final Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.miaozhen.com");
        props.put("mail.user", "dmp-alert@miaozhen.com");
        props.put("mail.password", "gshzj@2013");
        props.put("mail.sendto", "zhaopeng@miaozhen.com");

        String[] toaddr = {
               // "like@miaozhen.com", "4s@miaozhen.com", "zhaopeng@miaozhen.com"
                "zhaopeng@miaozhen.com", "654018509@qq.com"
        };

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String username = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(username, password);
            }
        };

        Session mailSession = Session.getInstance(props, auth);
        MimeMessage message = new MimeMessage(mailSession);

        InternetAddress from = new InternetAddress(props.getProperty("mail.user"));

        String toList = getMailList(toaddr);

        InternetAddress[] iaToList = new InternetAddress().parse(toList);
        InternetAddress to = new InternetAddress(props.getProperty("mail.sendto"));
        message.setFrom(from);
        message.setRecipients(RecipientType.TO, iaToList);


        // 设置邮件标题
        message.setSubject("昌荣-标签传输-量级检查");

        message.setContent(msg.toString(), "text/html;charset=UTF-8");

        // 发送邮件
        Transport trans = mailSession.getTransport("smtp");
        trans.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"), props.getProperty("mail.password"));
        trans.sendMessage(message, message.getAllRecipients());
        trans.close();
    }

    private String getMailList(String[] mailArray) {

        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();

    }
}
